//
//  OJLoginViewController.m
//  openJira
//
//  Created by Vincent Demay on 04/05/10.
//  Copyright 2010 Goojet. All rights reserved.
//

#import "OJLoginViewController.h"
#import "OJConnector.h"
#import "UserData.h"

@implementation OJLoginViewController
@synthesize delegate;

#define IMAGE_WIDTH 130
#define IMAGE_HEIGHT 58


- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
		[self.tableView setScrollEnabled:YES];
		self.tableView.allowsSelection = NO;
		self.view.backgroundColor = BG_COLOR;
    }
    return self;
}

- (void) loadView {
	[super loadView];
	
	UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
	UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.frame.size.width - IMAGE_WIDTH)/2, 20, IMAGE_WIDTH, IMAGE_HEIGHT)];
	img.image = [UIImage imageNamed:@"logo.png"];
	[header addSubview:img];
	self.tableView.tableHeaderView = header;
	[header release];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	// try to log in
	[UserData sharedData].password = password.text;
	[UserData sharedData].login = login.text;
	
	if ([[OJConnector getDefault] loginWithUser:login.text andPassword:password.text]) {
		[UserData save];
		//ok so login user
		[delegate onLoggedIn:self];
	} else {
		//TODO :say an error
	}
	
	return NO;
}

#pragma mark Table view methods


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 40;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
		
		if (indexPath.row == 0) {
			login = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width - 10, 30)];
			login.placeholder = @"Login";
			login.keyboardAppearance = UIKeyboardAppearanceAlert;
			login.returnKeyType = UIReturnKeyGo;
			login.delegate = self;
			[cell.contentView addSubview:login];
			[login becomeFirstResponder];
		} else {
			password = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width - 10, 30)];
			password.secureTextEntry = YES;
			password.placeholder = @"Password";
			password.keyboardAppearance = UIKeyboardAppearanceAlert;
			password.returnKeyType = UIReturnKeyGo;
			password.delegate = self;
			[cell.contentView addSubview:password];
		}
	}
	return cell;
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[login release];
	[password release];
    [super dealloc];
}


@end
